package com.jstnvr.smartcampus.controller;

import com.jstnvr.smartcampus.entity.Student;
import com.jstnvr.smartcampus.entity.Test;
import com.jstnvr.smartcampus.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private Environment env;

    @ResponseBody
    @PostMapping("/add")
    public int addTest(Test test){
        return testService.addTest(test);
    }

    @ResponseBody
    @GetMapping("/all")
    public Object findAllTest(
            @RequestParam(name = "pageNum", required = false, defaultValue = "1")
                    int pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                    int pageSize){
        //return userService.findAllUser(pageNum,pageSize);
            return testService.findAllTest(pageNum,pageSize);
    }





    /*
     * 第一个测试方法
     * */
    @ResponseBody
    @RequestMapping("/info")
    public String info() {

        return "hello world" + env.getProperty("pagehelper.helperDialect");

    }

    @GetMapping("/login.html")
    public String longin() {

        return "login";

    }


    @RequestMapping("/getName")
    public String getName(HttpServletRequest request) {

        request.setAttribute("name", "刘柱");

        return "/getName";


    }

    @RequestMapping("/getList")
    public String getList(Model model) {

        List<Student> list = new ArrayList<>();
        list.add(new Student("张大王", 23, "河北"));
        list.add(new Student("张小王", 78, "南京"));
        list.add(new Student("王帽子", 34, "西瓜"));

        model.addAttribute("list", list);
        return "list";

    }

    @PostMapping("/logins")

    public String login(HttpServletRequest request) {

        String usrName = request.getParameter("mobileNum");

        String password = request.getParameter("password");

        System.out.print("----" + usrName + "/////" + password);

        request.setAttribute("name", usrName);

        return "getName";

    }


}

